package com.yulan.subString;

import org.junit.Test;

/**
 * 67. 二进制求和
 */
public class LeetCode_67 {
    public String addBinary(String a, String b) {
        StringBuilder ans = new StringBuilder();

        int n = Math.max(a.length(),b.length());
        int carry = 0;
        for(int i = 0; i<n; i++){
            carry += i < a.length() ? a.charAt(a.length() - i - 1) - '0' : 0;
            carry += i < b.length() ? b.charAt(b.length() - i - 1) - '0' : 0;
            ans.append((char) (carry % 2 + '0'));
            carry /= 2;
        }

        if(carry > 0)
            ans.append('1');


        return ans.reverse().toString();
    }

    @Test
    public void test(){
        String a = "11";
        String b = "1";
        System.out.println(addBinary(a,b));
    }
}















